package com.kpmg.datalake.common.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.kpmg.datalake.common.enums.PersonParamEnum;
import com.kpmg.datalake.common.enums.UseOrDelEnum;
import com.kpmg.datalake.common.model.PersonParamsSetting;
import com.kpmg.datalake.common.service.IPersonParamsSettingService;
import com.kpmg.datalake.common.utils.ContextHolderUtils;
import com.kpmg.datalake.common.vo.PersonParamVO;
import com.kpmg.datalake.common.vo.ServerResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.i18n.SessionLocaleResolver;
import org.springframework.web.util.WebUtils;

/**
 * <p>
 * 个性化参数设置 前端控制器
 * </p>
 *
 * @author tzhang13
 * @since 2018-08-21
 */
@RestController
@RequestMapping("/personParam")
public class PersonParamsSettingController extends BaseController {

  private static final Logger logger = LoggerFactory.getLogger(PersonParamsSettingController.class);

  @Resource
  private IPersonParamsSettingService personParamsSettingService;


  /**
   * 查询数据字典
   */
  @PostMapping("/getPersonParamListByCode")
  public ServerResponse<Page<PersonParamVO>> getPersonParamListByCode(
      @RequestBody PersonParamVO personParamVO) {
    Page<PersonParamVO> paramPageByType;
    try {
      // 获取请求记录列表
      String langCd =
          StringUtils.isBlank(personParamVO.getLangCd()) ? ContextHolderUtils.getLocale().toString()
              : personParamVO.getLangCd();
      paramPageByType = new Page<>();
      paramPageByType.setCurrent(personParamVO.getPageNum());
      paramPageByType.setSize(personParamVO.getPageSize());
      paramPageByType = personParamsSettingService.getPersonParamPageByType(paramPageByType, langCd,
          personParamVO);

    } catch (Exception e) {
      return super.doError(e);

    }
    return super.doSuccess(paramPageByType);
  }

  /**
   * 保存个性化参数
   *
   * @author tzhang13
   * @since 2018-08-28
   */
  @PostMapping(value = "/saveOrUpdateParam")
  public ServerResponse<String> saveOrUpdateParam(
      @RequestBody PersonParamsSetting personParamSetting, HttpServletRequest request) {
    ServerResponse<String> stringServerResponse = personParamsSettingService
        .saveOrUpdateParam(personParamSetting);
    //设置当前session的国际化语言
    if (stringServerResponse.isSuccess() && PersonParamEnum.AcceptLang.getCode()
        .equals(personParamSetting.getParamsType())) {
      WebUtils.setSessionAttribute(request, SessionLocaleResolver.LOCALE_SESSION_ATTRIBUTE_NAME,
          new Locale(personParamSetting.getParamsValue()));

    }
    return stringServerResponse;
  }

  /**
   * vba 个性化设置保存
   *
   * @param personParamsSetting 个性化参数
   * @return ServerResponse
   */
  @PostMapping("/vbaSavePersonParam")
  public ServerResponse<String> vbaSavePersonParam(@RequestBody PersonParamsSetting personParamsSetting, HttpServletRequest request) {
    ServerResponse<String> stringServerResponse = personParamsSettingService.vbaSavePersonParams(personParamsSetting);
    //设置当前session的国际化语言
    if (stringServerResponse.isSuccess() && PersonParamEnum.AcceptLang.getCode()
            .equals(personParamsSetting.getParamsType())) {
      WebUtils.setSessionAttribute(request, SessionLocaleResolver.LOCALE_SESSION_ATTRIBUTE_NAME,
              new Locale(personParamsSetting.getParamsValue()));

    }
    return stringServerResponse;
  }

  @PostMapping("/getPersonParam")
  public ServerResponse<String> getPersonParam(@RequestBody PersonParamsSetting personParamsSetting) {
    return personParamsSettingService.getPersonParam(personParamsSetting);
  }

  /**
   * 获取人员所有个性化设置
   *
   * @return 个性化设置列表
   */
  @PostMapping("/getPersonParamList")
  public ServerResponse<ArrayList<PersonParamsSetting>> getPersonParamList() {
    List<PersonParamsSetting> settingList;
    try {
      PersonParamsSetting setting = new PersonParamsSetting();
      setting.setDelInd(UseOrDelEnum.USE.getIntCode());
      setting.setUserName(ContextHolderUtils.getUserName());
      settingList = personParamsSettingService
          .selectList(new EntityWrapper<>(setting));
    } catch (Exception e) {
      logger.error(e.getMessage(), e);
      return super.doError(e.getMessage());
    }
    return super.doSuccess(new ArrayList<>(settingList));
  }

}

